package main

import (
	"cow_golang/shared_mod/jsoncache"
	"fmt"
	"strconv"
	"time"

	"github.com/gin-gonic/gin"

	"cow_golang/shared_mod/config"
	"cow_golang/shared_mod/logger"
	"cow_golang/shared_mod/orm"

	"cow_golang/auth_server/internal/domain"
)

/*
1 注册账号
2 获取服务列表（含最近登录服务信息）
3 补丁版本更新
*/
func main() {
	//加载配置
	// 初始化配置管理器
	c := config.GetInstance()
	// 设置配置文件路径（可选）
	err := c.SetConfigPath("E:/goProject/src/cow_golang/auth_server/configs/application.yaml")
	if err != nil {
		fmt.Printf("Error setting config path: %v\n", err)
		return
	}
	// 加载配置
	err = c.InitConfig()
	if err != nil {
		fmt.Printf("Error initializing config: %v\n", err)
		return
	}
	// 获取配置项
	appName := c.GetString("app.name")
	appPort := c.GetInt64("app.port")
	// 初始化日志
	log := logger.GetInstance()
	log.Infof("App Name:%s port:%d", appName, appPort)
	// 初始化db
	host := c.GetString("database.host")
	domainPath := c.GetString("database.path")
	autoFlag := c.GetBool("database.autoFlag")
	orm.InitConfig(host, domainPath, autoFlag)
	mydb := orm.GetInstance()
	//测试orm
	// 初始化 Roles
	roles := domain.Roles{"Developer", "Designer"}

	// 初始化 JobInfo
	jobInfo := domain.Job{
		Title:   "Software Engineer",
		Salary:  100000,
		Company: "Moonshot AI",
	}

	// 初始化 Contracts
	contracts := map[string]interface{}{
		"contract1": map[string]string{
			"type":  "Full-time",
			"start": "2023-01-01",
		},
		"contract2": map[string]string{
			"type":  "Part-time",
			"start": "2023-06-01",
		},
	}
	// 初始化 Name
	name := "John Doe"
	// 初始化 Player
	player := domain.Player{
		ID:          1, // 假设 ID 是 1
		Name:        name,
		Roles:       roles,
		Contracts:   contracts,
		JobInfo:     jobInfo,
		CreatedTime: time.Now().Unix(), // 当前时间的 Unix 时间戳
	}
	fmt.Printf("org player:%v\n", player)
	//err = mydb.Create(player)
	//if err != nil {
	//	fmt.Println("创建用户失败:", err)
	//	return
	//}
	var findPlayer domain.Player
	err = mydb.Read(&findPlayer, "id = ?", 1)
	if err != nil {
		fmt.Printf("not find", err)
	}
	fmt.Printf("found player:%+v\n", findPlayer)
	// json格式数据缓存
	cache := jsoncache.GetInstance()
	// 加载数据目录
	jsonPath := c.GetString("json.path")
	if err := cache.LoadDir(jsonPath); err != nil {
		panic(err)
	}
	//初始化web服务(主要是路由)

	//初始化gin
	r := gin.Default()
	//启动gin
	r.Run(":" + strconv.FormatInt(appPort, 10))
}
